from datetime import datetime

from app import db


class BaseModel(db.Model):
    __abstract__ = True
    create_time = db.Column(db.DateTime, default=datetime.now)
    update_time = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now)


class User22(BaseModel):
    __tablename__ = 'user_tb'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(32), unique=True, nullable=False)
    real_name = db.Column(db.String(32))
    mobile = db.Column(db.String(11), unique=True, nullable=False)
    area_id = db.Column(db.Integer, db.ForeignKey('area_tb.id'))

    def __repr__(self):
        return self.name

    def to_dict(self):
        user_dict = {
            'id': self.id,
            'name': self.name,
            'real_name': self.real_name,
            'mobile': self.mobile,
            'area_id': self.area_id
        }
        return user_dict


class Area(BaseModel):
    __tablename__ = 'area_tb'
    __table_args__ = {"useexisting": True}

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(50), nullable=True)
    users = db.relationship('User22', backref='area_tb')

    def __repr__(self):
        return self.name

    def to_dict(self):
        area_dict = {
            'aid': self.id,
            'name': self.name,
        }
        return area_dict

#
# # 一人对多书
# class Book(db.Model):
#     # __table_args__ = {"useexisting": True}
#
#     id = db.Column(db.Integer, primary_key=True)
#     name = db.Column(db.String(50))
#     author_id = db.Column(db.Integer, db.ForeignKey('author.id'))
#
#
# class Author(db.Model):
#     # __table_args__ = {"useexisting": True}
#
#     id = db.Column(db.Integer, primary_key=True)
#     name = db.Column(db.String(80))
#     books = db.relationship('Book', backref='author')